package cn.com.xinjinfu.pdoulcProduct.utils;

import android.app.Activity;
import android.content.Context;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;

import java.util.Map;

import cn.com.xinjinfu.pdoulcProduct.common.ConstantValues;
import cn.com.xinjinfu.pdoulcProduct.engine.DataHandler;
import cn.com.xinjinfu.pdoulcProduct.entity.User;
import cn.com.xinjinfu.pdoulcProduct.manager.AccountManager;
import cn.com.xinjinfu.pdoulcProduct.manager.JSONManager;
import cn.com.xinjinfu.pdoulcProduct.manager.L;
import cn.com.xinjinfu.pdoulcProduct.manager.ToastManager;

/**
 * 刷新保存的User信息工具类
 * Created by android-adt on 2016/4/28.
 */
public class ReloadUserInfo {
    private RequestQueue requen;
    private Activity context;

    public ReloadUserInfo(Activity context){
        this.context = context;
        requen = Volley.newRequestQueue(context);
    }
    /**
     * 刷新保存的User信息网络请求
     */
    public void userRequest() {
        User user = AccountManager.getInstance().getUser();
        if (user != null) {
            final Map<String, String> parameters = DataHandler.getParameters("1404");
            parameters.put("id", user.getCurrUser().getSign());
            JsonObjectRequest request = new JsonObjectRequest(DataHandler.getBuildUrl(parameters), null, usersucceedRespon, null);
            request.setRetryPolicy(new DefaultRetryPolicy(ConstantValues.TIMEOUT, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
            requen.add(request);
        }
    }

    private Response.Listener<JSONObject> usersucceedRespon = new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject jobj) {
            L.i("刷新保存的User信息==================" + jobj.toString());
            if (JSONManager.getError(jobj)==-1){
                AccountManager.getInstance().saveUserInfoToFile(jobj);
            }else {
                ToastManager.showShort(context,JSONManager.getMsg(jobj));
            }
        }
    };
}
